package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.BuyRecordInfo;

/**
 * 购买历史Adapter
 * 
 * @author zhuchen
 * 
 */
public class PurchaseHistoryAdapter extends BaseAdapter implements OnClickListener {
    private List<BuyRecordInfo> list;
    private LayoutInflater inflater;

    public PurchaseHistoryAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<BuyRecordInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_purchasehistory, null);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.purchasehistory_title_image =
                    (ImageView) view.findViewById(R.id.purchasehistory_title_image);
            holder.purchasehistory_author_text =
                    (TextView) view.findViewById(R.id.purchasehistory_author_text);
            holder.purchasehistory_bookname_text =
                    (TextView) view.findViewById(R.id.purchasehistory_bookname_text);
            holder.purchasehistory_date_text =
                    (TextView) view.findViewById(R.id.purchasehistory_date_text);
            holder.purchasehistory_details =
                    (TextView) view.findViewById(R.id.purchasehistory_details);
            view.setTag(holder);
        }

        SqImageManager.getImageManager().displayImage(list.get(position).getImgUrl(),
                holder.purchasehistory_title_image, SqImageManager.IMG_DEFAULT);
        holder.purchasehistory_bookname_text.setText(list.get(position).getBookName());
        holder.purchasehistory_author_text.setText(list.get(position).getAnthor());
        if (!TextUtils.isEmpty(list.get(position).getTime())) {
            holder.purchasehistory_date_text.setText(list.get(position).getTime());
        }

        if (mHistoryAdapterListener != null && !TextUtils.isEmpty(list.get(position).getType())) {
            if ("2".equals(list.get(position).getType())) {
                holder.purchasehistory_details.setVisibility(View.VISIBLE);
                holder.purchasehistory_details.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHistoryAdapterListener.onClickDetails(list.get(position).getBookId(), list
                                .get(position).getChapterTotal());
                    }
                });
            } else {
                holder.purchasehistory_details.setVisibility(View.GONE);
            }
        }

        return view;
    }

    public onPurchaseHistoryAdapterListener mHistoryAdapterListener;

    public void setmHistoryAdapterListener(onPurchaseHistoryAdapterListener mHistoryAdapterListener) {
        this.mHistoryAdapterListener = mHistoryAdapterListener;
    }

    public interface onPurchaseHistoryAdapterListener {
        public void onClickDetails(String bid, String chapterTotal);
    }

    private static class ViewHolder {
        ImageView purchasehistory_title_image; // 书封面图片
        TextView purchasehistory_bookname_text; // 书名
        TextView purchasehistory_author_text; // 作者
        TextView purchasehistory_date_text; // 时间
        TextView purchasehistory_details;// 查看详细
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.purchasehistory_details) {

        }
    }
}
